﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// EditJump 的摘要说明。
    /// </summary>
    public partial class EditJumpPage : Page
    {
        private int _flowId;
        private int _stepId;
        protected const int ChangeFlowStepID = -1;

        protected override void OnInit(EventArgs e)
        {
            _flowId = QSUtil.GetPositiveInt32("FlowID");
            _stepId = QSUtil.GetPositiveInt32("StepID");

            cmdInsert.Click += cmdInsert_Click;
            btnReturn.RedirectUrl = "ManageFlow.aspx?FlowID=" + _flowId;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind();
            }

            base.OnLoad(e);
        }

        #region Bind
        private void Bind()
        {
            dgJumpList.DataSource = FlowJump.GetJumps(_flowId, _stepId);
            dgJumpList.DataBind();

            InitHeadLine();
            FillFlow();
            FillStep(ddStep, _flowId);
            FillFieldName(ddFieldName, _flowId);
            if (ddFieldName.Items.Count < 1 || ddStep.Items.Count < 1)
            {
                cmdInsert.Enabled = false;
            }
            else
            {
                cmdInsert.Enabled = true;
            }
        }

        private void FillFieldName(DropDownList ddField, int iFlowID)
        {
            ddField.Items.Clear();

            var list = FlowStyleDescription.GetStyleDescriptionByFlow(iFlowID, true);
            foreach (FlowStyleDescription item in list)
            {
                ddField.Items.Add(new ListItem(item.Description, item.Name));
            }

            ddField.Items.Add(new ListItem("《职级》", FlowStyleDescription.Caste));
        }

        private void FillFlow()
        {
            ddFlow.Items.Clear();
            foreach (Flow flow in Flow.GetAllFlows())
            {
                ddFlow.Items.Add(new ListItem(flow.Name, flow.ID.ToString()));
            }
        }

        private void FillStep(DropDownList dropDownList, int iFlowID)
        {
            var steps = FlowStep.GetSteps(iFlowID);
            dropDownList.Items.Clear();
            foreach (var step in steps)
            {
                dropDownList.Items.Add(new ListItem(step.Name, step.ID.ToString()));
            }

            dropDownList.Items.Add(new ListItem("《结束文档》", FlowStep.EndStepID.ToString()));
            dropDownList.Items.Add(new ListItem("《跳转到其他流程》", ChangeFlowStepID.ToString()));
        }

        private void InitHeadLine()
        {
            labTitle.Text = Flow.GetFlowTitle(_flowId) + "->" + FlowStep.GetStepTitle(_flowId, _stepId);
        }
        #endregion Bind

        #region event handlers
        private void cmdInsert_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                string fieldName = ddFieldName.Items[ddFieldName.SelectedIndex].Value;
                int stepId = Int32.Parse(ddStep.Items[ddStep.SelectedIndex].Value);
                int flowId = 0; // by default use the current flow
                if (stepId == ChangeFlowStepID)
                {
                    if (ddFlow.Items.Count == 0)
                    {
                        SiteContext.Current.Alert("请选择要跳转的流程");
                        return;
                    }

                    stepId = 0;
                    flowId = int.Parse(ddFlow.Items[ddFlow.SelectedIndex].Value);
                }

                FlowJump.AddJump(
                    _flowId,
                    _stepId,
                    fieldName,
                    ddComparisonMode.SelectedMode,
                    Double.Parse(txtContant.Text),
                    flowId,
                    stepId,
                    ddlFlowRule.SelectedRule);
            }

            Bind();
        }

        public void MyGrid_Delete(object sender, DataGridCommandEventArgs e)
        {
            int priority = int.Parse(dgJumpList.DataKeys[e.Item.ItemIndex].ToString());

            FlowJump.DeleteJump(_flowId, _stepId, priority);
            Bind();
        }

        public void MyGrid_Move(object sender, DataGridCommandEventArgs e)
        {
            int priority = int.Parse(dgJumpList.DataKeys[e.Item.ItemIndex].ToString());

            FlowJump.MoveUpJump(_flowId, _stepId, priority);
            Bind();
        }

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgJumpList.CurrentPageIndex = e.NewPageIndex;
        }
        #endregion event handlers
    }
}